import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/Home'
import store from "@/store"

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import('../views/Goods.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/User.vue'),
    redirect: "/user/cart",
    children: [
      {
        path: 'cart', component: () => import('../components/user/Cart.vue')
      }
    ]
    // beforeEnter: (to, from, next) => {
    //   if (to.path == "/user") {
    //     let token = localStorage.getItem("x-auth-token")
    //     if (token) {
    //       next()
    //     } else {
    //       store.dispatch("Toast/asyncIsShowToast", {
    //         isshow: true,
    //         msg: "请先登录!",
    //         type: "danger"
    //       })
    //     }
    //   } else {
    //     next()
    //   }
    // },

  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/Order.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import('../views/Free.vue')
  },
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/Details.vue')
  },
  {
    path: '*',
    name: 'error',
    component: () => import('../components/Error.vue')
  },
]


const router = new VueRouter({
  mode: 'history',
  routes
})
//屏蔽重复路由警告
const VueRouterPush = router.push
router.push = function push(to) {
  undefined
  return VueRouterPush.call(this, to).catch(err => err)
}
// 全局导航守卫

// router.beforeEach((to, from, next) => {
//   if (to.path == "/user") {
//     let token = localStorage.getItem("x-auth-token")
//     if (token) {
//       next()
//     } else {
//       store.dispatch("Toast/asyncIsShowToast", {
//         isshow: true,
//         msg: "请先登录!",
//         type: "danger"
//       })
//     }
//   } else {
//     next()
//   }
// })
export default router
